package common.pojo;

/**
 * Created by yangqy on 21/12/2016.
 */
public class SdkResult<T> {

    public static final int SUCCESS = 0;

    public static final int FAILS = -1;

    public static final int ACCOUNT_LOCK=1003;
    public static final int ACCOUNT_CANCEL=1003;

    private int code;

    private String msg;

    private Object transientData;

    T data;

    public boolean isSuccess(){
        return code==SUCCESS;
    }

    public boolean isError(){
        return code!=SUCCESS;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static SdkResult returnDataWrapper(Object... obj) {
        return returnSuccessWrapper(obj);
    }

    public static SdkResult returnWrapper(int code, String message, Object... obj) {
        SdkResult result = new SdkResult();
        result.setMsg(message);
        if (obj != null && obj.length == 1) {
            result.setData(obj[0]);
        } else {
            result.setData(obj);
        }
        result.setCode(code);
        return result;
    }

    public static SdkResult returnBoolWrapper(boolean bool, String fails, Object... obj) {
        if (bool) {
            return returnSuccessWrapper(obj);
        }
        return returnFailsWrapper(fails, obj);
    }

    public static SdkResult returnSuccessWrapper(Object... obj) {
        return returnWrapper(SdkResult.SUCCESS, "success", obj);
    }

    public static SdkResult returnFailsWrapper(String message, Object... obj) {
        return returnWrapper(SdkResult.FAILS, message, obj);
    }

    public Object getTransientData() {
        return transientData;
    }

    public SdkResult<T> setTransientData(Object transientData) {
        this.transientData = transientData;
        return this;
    }
}